package team.yidong.fenda.serviceImpl;

import com.qiniu.util.Json;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import team.yidong.fenda.DTO.HospitalDepartmentDTO;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalDepartment;
import team.yidong.fenda.repository.HospitalDepartmentRepository;
import team.yidong.fenda.repository.HospitalRepository;
import team.yidong.fenda.service.HospitalDepartmentService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2018/3/17.
 */
@Service
public class HospitalDepartmentServiceImpl implements HospitalDepartmentService {
    @Autowired
    HospitalDepartmentRepository hospitalDepartmentRepository;
    @Autowired
    HospitalRepository hospitalRepository;

    @Override
    public JSONArray findHospitalDepartment(Integer hospitalId) {
        List<HospitalDepartment> hospitalDepartmentList = hospitalDepartmentRepository.findByHospitalId(hospitalId);

        return JSONArray.fromObject(hospitalDepartmentList);
    }

    @Override
    public JSONArray findAll(String hospitalName) {
        List<HospitalDepartment> hospitalDepartmentList = hospitalDepartmentRepository.findAll(new Specification<HospitalDepartment>() {
            List<Predicate> predicateList = new ArrayList<>();

            @Override
            public Predicate toPredicate(Root<HospitalDepartment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if (hospitalName != null && !hospitalName.equals("")) {
                    Hospital hospital = hospitalRepository.findByName(hospitalName);
                    Predicate p = criteriaBuilder.equal(root.get("hospitalId"), hospital.getId());
                    predicateList.add(p);
                }
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        });
        List<HospitalDepartmentDTO> hospitalDepartmentList1 = new ArrayList<>();
        for (HospitalDepartment hospitalDepartment : hospitalDepartmentList) {
            HospitalDepartmentDTO hospitalDepartmentDTO = new HospitalDepartmentDTO();
            hospitalDepartmentDTO.setHospitalName(hospitalRepository.findOne(hospitalDepartment.getHospitalId()).getName());
            hospitalDepartmentDTO.setDepartmentName(hospitalDepartment.getDepartmentName());
            hospitalDepartmentDTO.setHospitalId(hospitalDepartment.getHospitalId());
            hospitalDepartmentDTO.setDepartmentId(hospitalDepartment.getId());
            hospitalDepartmentList1.add(hospitalDepartmentDTO);
        }
        return JSONArray.fromObject(hospitalDepartmentList1);
    }

    @Transactional
    @Override
    public void addHospitalDepartment(String hospitalName, String departmentName) {
        Hospital hospital = hospitalRepository.findByName(hospitalName);
        HospitalDepartment hospitalDepartment = new HospitalDepartment();
        hospitalDepartment.setHospitalId(hospital.getId());
        hospitalDepartment.setDepartmentName(departmentName);
        hospitalDepartmentRepository.save(hospitalDepartment);
    }

    @Override
    public JSONArray findAllDepartment() {
        return JSONArray.fromObject(hospitalDepartmentRepository.findAll());
    }

    @Override
    public JSONArray findByHospitalName(String hospitalName) {
        Hospital hospital = hospitalRepository.findByName(hospitalName);
        List<HospitalDepartment> hospitalDepartmentList = hospitalDepartmentRepository.findByHospitalId(hospital.getId());
        return JSONArray.fromObject(hospitalDepartmentList);
    }
}
